Java RESTful Engine Custom Post Processing Example
The Java RESTful Engine Custom Processing plugin allows custom modification of reports prior to them being marked as complete and ready for delivery. This code example implements the IDocumentPostProcessor
interface to modify basic PDF metadata using the Apryse Java SDK. For more information on the Apryse Java SDK API you can follow this link. You can also find more information on the Java RESTful Custom Processing functionality here including how to set up and install the custom processor.
Requirements
- An Apryse Java SDK trial key
- The Java RESTful Custom Processing pre-requisites
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>CustomPostProcessor</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<repositories>
<!-- The repository for the Fluent Post Processor interfaces -->
<repository>
<id>windward-maven-repo</id>
<name>windward-maven-repo</name>
<url>https://windward.mycloudrepo.io/public/repositories/windward-libs</url>
</repository>
<!-- The repository for the Apryse Java SDK -->
<repository>
<id>apryse-maven-repo</id>
<name>apryse-maven-repo</name>
<url>https://pdftron.com/maven/release</url>
</repository>
</repositories>
<dependencies>
<!-- The Fluent Post Processor interface -->
<dependency>
<groupId>net.windward</groupId>
<artifactId>FluentCustomProcessing</artifactId>
<version>LATEST</version>
</dependency>
<!-- The Aprsye Java SDK -->
<dependency>
<groupId>com.pdftron</groupId>
<artifactId>PDFNet</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
CustomPostProcessor.java
package org.example;
import FluentCustomProcessing.CustomDocument;
import FluentCustomProcessing.IDocumentPostProcessor;
import FluentCustomProcessing.CustomProcessingTestUtil;
import com.pdftron.common.PDFNetException;
import com.pdftron.pdf.PDFDoc;
import com.pdftron.pdf.PDFDocInfo;
import com.pdftron.pdf.PDFNet;
import com.pdftron.sdf.SDFDoc;
import javax.xml.bind.JAXBException;
import java.io.*;
public class CustomPostProcessor implements IDocumentPostProcessor {
public CustomPostProcessor() {
PDFNet.initialize("Your-Apryse-SDK-Trial-Key");
}
/**
* A test method for the custom post processor.
* Useful for testing your functionality outside the context of the Fluent RESTful Engine.
* This method will process a test-custom-post-processor.pdf file and output a test-custom-post-processor-output.pdf file
* @param args
* @throws IOException
* @throws JAXBException
*/
public static void main(String[] args) throws IOException, JAXBException {
CustomPostProcessor testProcessor = new CustomPostProcessor();
//Use the CustomProcessingTestUtil to generate a document
CustomDocument doc = CustomProcessingTestUtil.getDocument("test-custom-post-processor.pdf");
CustomDocument retDoc = testProcessor.process(doc);
try (FileOutputStream fos = new FileOutputStream("test-custom-post-processor-output.pdf")){
fos.write(retDoc.getData());
}
}
/**
* The process method called by the Fluent RESTful Engine
* @param customDocument to be modified
* @return updated CustomDocument
*/
@Override
public CustomDocument process(CustomDocument customDocument) {
//Use the Apryse Java PDF SDK
try (PDFDoc doc = new PDFDoc(customDocument.getData())) {
PDFDocInfo docInfo = doc.getDocInfo();
//Modify Title, Subject, and Author as an example
docInfo.setTitle("Custom Post Processor Example");
docInfo.setSubject("This is an example of the Fluent Custom Post Processor.");
docInfo.setAuthor("Fluent Custom Post Processor");
//Save the document
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
doc.save(outputStream, SDFDoc.SaveMode.LINEARIZED, null);
//Save the document to our return object
customDocument.setData(outputStream.toByteArray());
} catch (PDFNetException | IOException e) {
throw new RuntimeException(e);
}
//Return the updated customDocument to the Fluent RESTful Engine
return customDocument;
}
}